package com.yjd.bi.fund.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.bi.fund.model.BiFundsStatisticsMinuteModel;
import com.yjd.bi.fund.service.IBiFundsStatisticsMinuteService;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.fund.dao.IBiFundsStatisticsMinuteDao;

/**
 * <b>description</b>：资金量记录业务实现<br>
 * <b>time</b>：2016-04-18 10:54:49 <br>
 * <b>author</b>：  chenpengfei@yijeidai.com
 */
@Service("biFundsStatisticsMinuteService")
public class BiFundsStatisticsMinuteServiceImpl extends BaseServiceImpl implements IBiFundsStatisticsMinuteService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BiFundsStatisticsMinuteModel insert(BiFundsStatisticsMinuteModel model) throws Exception {
		return this.biFundsStatisticsMinuteDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BiFundsStatisticsMinuteModel model) throws Exception {
		return this.biFundsStatisticsMinuteDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.biFundsStatisticsMinuteDao.deleteModel(paramMap, true);
	}

	public EChartsModel getDates(Long day,String field) throws Exception {
		// TODO Auto-generated method stub
		EChartsModel ec=new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(DateUtil.getFormateStringByDailyDimension(day, DateUtil.PATTERN_yyyy_MM_dd)));
		ec.addSeries(FrameUtil.newArrayList(null), DateUtil.getFormateStringByDailyDimension(day, DateUtil.PATTERN_yyyy_MM_dd));
		List<BiFundsStatisticsMinuteModel> m=this.getModelList(FrameUtil.newHashMap("start",day.longValue(),"end",DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(""+day, "yyyyMMddHHmmss")+DateUtil.DAY_IN_SECOND,"yyyyMMddHHmmss")), DbWREnums.READ);
		for(BiFundsStatisticsMinuteModel model:m){
			ec.getSeries().get(0).getData().add(NumberUtil.formatNumber(model.getAttrByField(field),"##0.00"));
		}
		for(String d:TimeDimensionUtil.getDailyDemention()){
			ec.getXAxis().add(d);
			if(ec.getSeries().get(0).getData().size()<ec.getXAxis().size()){
				ec.getSeries().get(0).getData().add("0.00");
			}
		}
		return ec;
	}
	
	@Resource
	private IBiFundsStatisticsMinuteDao biFundsStatisticsMinuteDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.biFundsStatisticsMinuteDao;
	}

}
